/*
 * Copyright: Copyright (c) 2014-2015 ZhongDaMen,Inc.All Rights Reserved. 
 * Company:中大门网络科技有限公司
 */

package com.webdemo.util;

import org.springframework.util.StringUtils;  
import java.beans.PropertyEditorSupport;  
import java.text.DateFormat;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;

/**
 * <h1>Spring MVC日期编辑器</h1>
 * @ClassName: DateEditor
 * @Description: TODO
 * @author Zdm-lifang
 * @date 2015年8月27日 下午6:57:23
 *
 */
public class DateEditor extends PropertyEditorSupport {
	private static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");  
    private static final DateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  
    private DateFormat dateFormat;  
    private boolean allowEmpty = true;  
  
    public DateEditor() {  
    }  
  
    public DateEditor(DateFormat dateFormat) {  
        this.dateFormat = dateFormat;  
    }  
  
    public DateEditor(DateFormat dateFormat, boolean allowEmpty) {  
        this.dateFormat = dateFormat;  
        this.allowEmpty = allowEmpty;  
    }  
  
    /** 
     * Parse the Date from the given text, using the specified DateFormat. 
     */  
    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        if (this.allowEmpty && !StringUtils.hasText(text)) {  
            // Treat empty String as null value.  
            setValue(null);  
        } else {  
            try {  
                if(this.dateFormat != null)  
                    setValue(this.dateFormat.parse(text));  
                else {  
                    if(text.contains(":"))  
                        setValue(TIMEFORMAT.parse(text));  
                    else  
                        setValue(DATEFORMAT.parse(text));  
                }  
            } catch (ParseException ex) {  
                throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);  
            }  
        }  
    }  
  
    /** 
     * Format the Date as String, using the specified DateFormat. 
     */  
    @Override  
    public String getAsText() {  
        Date value = (Date) getValue();  
        DateFormat dateFormat = this.dateFormat;  
        if(dateFormat == null)  
            dateFormat = TIMEFORMAT;  
        return (value != null ? dateFormat.format(value) : "");  
    }  
}
